import { defineStore } from "pinia";
import { ref } from "vue";
import { blocks as blocksData } from "@/mocks/blocks";
import { BlockItem } from "./helper";

const useBlocksStore = defineStore('blocks', () => {
    const blocks = ref<BlockItem[]>(blocksData);
    const currentBlockId = ref('');

    function setCurrentBlockId(id: string) {
        currentBlockId.value = id;
    }

    function updateBlocks(newBlocks: BlockItem[]) {
        console.log('newBlocks', newBlocks);
        blocks.value = newBlocks;
    }

    function updateBlock(id: string, newBlock: Partial<BlockItem>) {
        const index = blocks.value.findIndex(block => block.id === id);
        if (index !== -1) {
            blocks.value[index] = { ...blocks.value[index], ...newBlock };
        }
    }

    return {
        blocks,
        currentBlockId,
        setCurrentBlockId,
        updateBlocks,
        updateBlock
    }
})


export default useBlocksStore;